using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using DBA.DataBaseAccess;
using Common.ExpressionAnalysis;

namespace BL.ServerBL
{
    partial class ServerRemotingCommon : BL.BBClientRemotingInterface.IBudgetBorrow
    {

        public bool IsOverdraft(string borrow_price, string budget_range_id,
            string department_id, string budget_type_id)
        {
            string budget_balance_sum = DBAccess.GetBudgetBalanceSum(budget_range_id, department_id, budget_type_id);
            return Convert.ToInt32(borrow_price) <= Convert.ToInt32(budget_balance_sum);
        }

        public bool BudgetBorrow(string user_id, string borrow_price,
            string budget_range_id, string department_id, string budget_type_id, 
            string borrow_remark)
        {
            int borrowsum = Convert.ToInt32(borrow_price);
            DataSet ds = DBAccess.GetBudgetInfo(budget_range_id, budget_type_id, department_id,
                null, null, null, true, true);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (borrowsum > 0)
                {
                    int budget_balance = Convert.ToInt32(dr["budget_balance"]);
                    if (budget_balance == 0)
                        continue;
                    if (budget_balance >= borrowsum )
                    {
                        DBAccess.ChangeBudgetBalance(dr["budget_id"].ToString(),
                            ((int)dr["budget_balance"] - borrowsum).ToString());
                        DBAccess.InsertBorrowInfo(user_id, borrowsum.ToString(), 
                            dr["budget_id"].ToString().Trim(), borrow_remark);
                        break;
                    }
                    else
                    {
                        DBAccess.ChangeBudgetBalance(dr["budget_id"].ToString(), "0");
                        DBAccess.InsertBorrowInfo(user_id, dr["budget_balance"].ToString().Trim(),
                            dr["budget_id"].ToString().Trim(),borrow_remark);
                        borrowsum -= (int)dr["budget_balance"];
                    }
                }
                else
                    break;

            }
            return true;
        }

        public bool BudgetBorrow(string user_id, string borrow_price,
            string budget_id, string borrow_remark)
        {
            DBAccess.ChangeBudgetBalanceSub(budget_id, (-1) * Convert.ToInt32(borrow_price));
            return DBAccess.InsertBorrowInfo(user_id, borrow_price, budget_id, borrow_remark);
        }

        public DataSet GetBorrowFullInfo(string user_id, bool is_allpayback)
        {
            return DBAccess.GetBorrowFullInfo(user_id, is_allpayback);
        }

        public DataSet GetBorrowFullInfo(bool is_allpayback)
        {
            return DBAccess.GetBorrowFullInfo(is_allpayback);
        }

        public DataSet GetBorrowBudgetItemFullInfo(string user_id, bool is_allpayback)
        {
            return DBAccess.GetBorrowBudgetItemFullInfo(user_id, is_allpayback);
        }

        public  DataSet GetBorrowFullInfo(string borrow_id)
        {
           return DBAccess.GetBorrowFullInfo(borrow_id);
        }

        public string GetBorrowPriceNotPayBack(string borrow_id)
        {
            return DBAccess.GetBorrowPriceNotPayBack(borrow_id);
        }
    }
}
